class AudioManager {
	constructor() {
		if (AudioManager.instance) {
			return AudioManager.instance;
		}
		AudioManager.instance = this;

		this.audioContext = uni.createInnerAudioContext();
		this.currentSong = {
			playlist: [],
			current: {}
		};
		this.playStatus = {
			isPlaying: false,
			currentTime: 0,
			playMode: 'loop' // loop, random, single
		};

		this.callbacks = {
			onTimeUpdate: [],
			onPlayStatusChange: [],
			onSongChange: []
		};

		this.initAudioListeners();
	}

	initAudioListeners() {
		this.audioContext.onTimeUpdate(() => {
			this.playStatus.currentTime = this.audioContext.currentTime;
			this.callbacks.onTimeUpdate.forEach(callback => callback(this.playStatus.currentTime));
		});

		this.audioContext.onEnded(() => {
			this.playNext();
		});

		this.audioContext.onError((res) => {
			console.error('音频播放错误：', res);
			this.playStatus.isPlaying = false;
			this.notifyPlayStatusChange();
			this.playNext();
		});
	}

	play() {
		if (!this.currentSong.current.url) return;
		this.audioContext.play();
		this.playStatus.isPlaying = true;
		this.notifyPlayStatusChange();
	}

	pause() {
		if (!this.currentSong.current.url) return;
		this.audioContext.pause();
		this.playStatus.isPlaying = false;
		this.notifyPlayStatusChange();
	}

	playNext() {
		if (!this.currentSong.playlist || this.currentSong.playlist.length === 0) return;

		let nextIndex;
		const currentIndex = this.currentSong.playlist.findIndex(song => song.id === this.currentSong.current.id);

		switch (this.playStatus.playMode) {
			case 'loop':
				nextIndex = (currentIndex + 1) % this.currentSong.playlist.length;
				break;
			case 'random':
				do {
					nextIndex = Math.floor(Math.random() * this.currentSong.playlist.length);
				} while (nextIndex === currentIndex && this.currentSong.playlist.length > 1);
				break;
			case 'single':
				nextIndex = currentIndex;
				break;
			default:
				nextIndex = (currentIndex + 1) % this.currentSong.playlist.length;
		}

		this.switchSong(this.currentSong.playlist[nextIndex]);
	}
	playPrev() {
		if (!this.currentSong.playlist || this.currentSong.playlist.length === 0) return;

		let prevIndex;
		const currentIndex = this.currentSong.playlist.findIndex(song => song.id === this.currentSong.current.id);

		switch (this.playStatus.playMode) {
			case 'loop':
				// 循环模式：上一曲索引 = (当前索引 - 1 + 列表长度) % 列表长度
				prevIndex = (currentIndex - 1 + this.currentSong.playlist.length) % this.currentSong.playlist
				.length;
				break;
			case 'random':
				// 随机模式：生成一个与当前不同的随机索引
				do {
					prevIndex = Math.floor(Math.random() * this.currentSong.playlist.length);
				} while (prevIndex === currentIndex && this.currentSong.playlist.length > 1);
				break;
			case 'single':
				// 单曲循环：上一曲还是当前歌曲
				prevIndex = currentIndex;
				break;
			default:
				// 默认使用循环模式逻辑
				prevIndex = (currentIndex - 1 + this.currentSong.playlist.length) % this.currentSong.playlist
				.length;
		}

		// 切换到上一曲
		this.switchSong(this.currentSong.playlist[prevIndex]);
	}
	switchSong(song) {
		this.currentSong.current = song;
		this.audioContext.src = song.url;
		this.playStatus.currentTime = 0;
		this.play();
		this.notifySongChange();
	}

	addToPlaylist(song) {
		const existingIndex = this.currentSong.playlist.findIndex(s => s.id === song.id);
		if (existingIndex === -1) {
			this.currentSong.playlist.push(song);
		}
	}

	setPlayMode(mode) {
		this.playStatus.playMode = mode;
		this.notifyPlayStatusChange();
	}

	// 注册回调函数
	onTimeUpdate(callback) {
		this.callbacks.onTimeUpdate.push(callback);
	}

	onPlayStatusChange(callback) {
		this.callbacks.onPlayStatusChange.push(callback);
	}

	onSongChange(callback) {
		this.callbacks.onSongChange.push(callback);
	}

	// 移除回调函数
	offTimeUpdate(callback) {
		const index = this.callbacks.onTimeUpdate.indexOf(callback);
		if (index > -1) {
			this.callbacks.onTimeUpdate.splice(index, 1);
		}
	}

	offPlayStatusChange(callback) {
		const index = this.callbacks.onPlayStatusChange.indexOf(callback);
		if (index > -1) {
			this.callbacks.onPlayStatusChange.splice(index, 1);
		}
	}

	offSongChange(callback) {
		const index = this.callbacks.onSongChange.indexOf(callback);
		if (index > -1) {
			this.callbacks.onSongChange.splice(index, 1);
		}
	}

	// 通知状态变化
	notifyPlayStatusChange() {
		this.callbacks.onPlayStatusChange.forEach(callback => callback(this.playStatus));
	}

	notifySongChange() {
		this.callbacks.onSongChange.forEach(callback => callback(this.currentSong));
	}

	// 获取当前状态
	getCurrentSong() {
		return this.currentSong;
	}

	getPlayStatus() {
		return this.playStatus;
	}
}

export default new AudioManager();